package com.sbx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sbx.entity.dto.Route;
import com.sbx.mapper.RouteMapper;
import com.sbx.service.RouteService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
public class RouteServiceImpl implements RouteService {

    @Resource
    private RouteMapper routeMapper;

    @Override
    public List<Route> getPath(String roleId) {
        //根据roleId parentId  查询到一级路由
        List<Route> routes = routeMapper.selectList(new LambdaQueryWrapper<Route>()
                .eq(Route::getRoleId, roleId)
                .eq(Route::getParentId, "root"));
        getChildren(routes);
        return routes;
    }

    private void getChildren(List<Route> routes) {

        if (CollectionUtils.isEmpty(routes)) {
            return;
        }
        for (Route route : routes) {

            List<Route> childrenList = routeMapper.selectList(new LambdaQueryWrapper<Route>().eq(Route::getParentId, route.getId()));
            if (!CollectionUtils.isEmpty(childrenList)) {
                route.setChildren(childrenList);
                getChildren(childrenList);
            }
        }
    }
}
